package com.yunnuo.system.menu.dao;

import com.yunnuo.system.menu.entity.Menu;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * Created by w_weihf on 2018/8/3.
 */
@Repository
@Transactional
public interface IMenuDao   extends JpaRepository<Menu,String> {

    /**
     * 根据用户id 查询菜单
     * @param userId
     * @return
     */
    @Query(value = " select  m " +
            " from Menu m " +
            " left join RoleMenu rm on m.id = rm.menuId " +
            " left join UserRole ur on rm.roleId = ur.roleId " +
            " where ur.userId =?1 and m.menuType in ('M', 'C') and m.visible = 0 " +
            " order by m.orderNum")
     List<Menu> selectAllMenuByUserId(String userId);

    /**
     * 根据用户id查询权限
     * @param userId
     * @return
     */
     @Query(value = " select  m.perms" +
            " from Menu m " +
            " left join RoleMenu rm on m.id = rm.menuId " +
            " left join UserRole ur on rm.roleId = ur.roleId " +
            " where ur.userId =?1 ")
     List<String> selectPermsByUserId(String userId);

     @Override
     List<Menu> findAll();

    /**
     * 根据父级id查询是否包含下属菜单
     * @param parentId
     * @return
     */
     List findAllByParentId(String parentId);


}
